package com.epfresh.supplier.widget;

import android.support.annotation.NonNull;
import android.util.Log;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.ContentEntity;
import com.epfresh.supplier.entity.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhuli on 2017/8/22.
 */

public abstract class RequestDeliveryActivity extends BaseActivity {
    private ArrayList<DeliveryItem> mDeliveryItem;


    /**
     * 加载通知列表数据
     */
    public void getDeliveryList(String mId) {

        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_customer_adresses);
        Map<String, String> map = new HashMap<>();
        map.put("accountId", mId);
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_customer_adresses, onRequestListener);
    }

    private OnRequestListener<DeliveryInfo> onRequestListener = new OnRequestListener<DeliveryInfo>() {
        @Override
        public DeliveryInfo jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, new TypeToken<DeliveryInfo>() {
            }.getType());
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<DeliveryInfo> re, Object tag) {
            hideProgressDialog();
            if(null != re) {
                mDeliveryItem = re.getResponseElement().getAddresses();
                Log.d("ces","onResponse 收货地址个数 = "+mDeliveryItem.size());
            }
            updateUI(mDeliveryItem);
        }
    };
    protected abstract void updateUI(ArrayList<DeliveryItem> mDeliveryItem);

   /* private ArrayList<DeliveryItem> mDeliveryItem;

    public void getDeliveryList(String mId) {
        RequestEntityMapSupply re = new RequestEntityMapSupply();
        re.setCmd(AppRouter.CMD_CUSTOMER_ADRESSES);
        re.putParameter("accountId", mId);

        ApplicationHelper.getInstance().requestPost(re)
                .converterAsync(DeliveryInfo.class)
                .doOnSubscribe(new Consumer<Dissupplierable>() {
                    @Override
                    public void accept(@NonNull Dissupplierable dissupplierable) throws Exception {
                        showLoading();
                    }
                })
                .subscribe(new ObserverResponse<DeliveryInfo>() {
                    @Override
                    public void onSubscribe(@NonNull Dissupplierable d) {
                        addDissupplierable(d);
                    }

                    @Override
                    public void onResponse(ResponseEntity<DeliveryInfo> re, Object tag) {
                        dismissLoading();
                        if(null != re) {
                            mDeliveryItem = re.getResponse().getAddresses();
                            Log.d("ces","onResponse 收货地址个数 = "+mDeliveryItem.size());
                        }
                        updateUI(mDeliveryItem);
                    }

                    @Override
                    public void onResponseError(ResponseEntity<DeliveryInfo> re, ResponseError error, Object tag) {
                        //服务器返回error信息
                        dismissLoading();
                    }

                    @Override
                    public void onError(ResponseError error, Object tag) {
                        //3种情况
                        //0x001,服务器返回body是空
                        //0x002,服务器返回数据解析异常
                        //0x003,抛出异常
                        Log.e("response", "==4:");
                        if (error.isBodyEmptyError()) {

                        } else if (error.isResponseDecodeError()) {

                        } else if (error.isThrowableError()) {

                        }
                        toast(getString(R.string.response_error));
                        dismissLoading();
                    }
                });
    }

    protected abstract void updateUI(ArrayList<DeliveryItem> mDeliveryItem);
*/
}




